package org.jeecg.modules.scm.service.impl;


import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.scm.dto.EditInvDto;
import org.jeecg.modules.scm.entity.ScmFinaContract;
import org.jeecg.modules.scm.entity.ScmInvestor;
import org.jeecg.modules.scm.mapper.ScmInvestorMapper;
import org.jeecg.modules.scm.service.IScmFinaContractService;
import org.jeecg.modules.scm.service.IScmInvestorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 资金方服务实现类
 * @Author: zzq
 * @Date: 2021-10-21
 * @Version: V1.0
 */
@Service
public class ScmInvestorServiceImpl extends ServiceImpl<ScmInvestorMapper, ScmInvestor> implements IScmInvestorService {

    @Autowired
    private IScmFinaContractService scmFinaContractService;

    @Override
    public void editById(EditInvDto dto) {

        ScmInvestor investor = new ScmInvestor();
        // 将dto 的属性复制给实体类
        BeanUtils.copyProperties(dto, investor);
        investor.setUpdateTime(new Date());

        baseMapper.updateById(investor);
    }

    @Override
    public List<Map<String, Object>> likeName(String key) {
        LambdaQueryWrapper<ScmInvestor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ScmInvestor::getFullName, ScmInvestor::getInvestorId)
                .like(ScmInvestor::getFullName, key);
        return baseMapper.selectMaps(queryWrapper);
    }

    @Override
    public String uploadAccessory(MultipartFile file, Integer invId) {
        ScmInvestor investor = lambdaQuery().select(ScmInvestor::getInvestorId, ScmInvestor::getAccessory)
                .eq(ScmInvestor::getInvestorId, invId).one();

        if (investor == null) {
            throw new JeecgBootException("资金方不存在");
        }
        String urls = investor.getAccessory();
        String url = CommonUtils.upload(file, "/investor", CommonConstant.UPLOAD_TYPE_MINIO);

        String path = URLUtil.getPath(url);

        if (StringUtils.isEmpty(urls)) {
            urls = path;
        } else {
            urls = urls + "," + path;
        }
        lambdaUpdate().eq(ScmInvestor::getInvestorId, invId)
                .set(StringUtils.isNotBlank(path), ScmInvestor::getAccessory, urls)
                .update();

        return urls;
    }

    @Override
    public List<ScmFinaContract> queryInvContractList(Integer invId) {
        List<ScmFinaContract> listByInvestorId = scmFinaContractService.getListByInvestorId(invId);

        return listByInvestorId;
    }
}
